package com.goldsprite.ruletilemap.gdxexamples;

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator;
import com.badlogic.gdx.utils.ScreenUtils;
import com.badlogic.gdx.utils.viewport.FitViewport;
import com.goldsprite.ruletilemap.core.RegularizationHandler;
import com.goldsprite.ruletilemap.core.RuleTile;

public class GdxDemo extends ApplicationAdapter {
	private SpriteBatch batch;
	private OrthographicCamera camera;
	private FitViewport viewport;

	private GdxMapRenderer mapRenderer;
	private RegularizationHandler handler;
	private BitmapFont font;

	// 地图数据
	private String fixedMapData;
	private String fixedProcessedMapData;
	private String currentRandomMapData;
	private String currentRandomProcessedMapData;

	// 渲染模式
	private int renderMode = 0; // 0:固定原始, 1:固定规则化, 2:随机原始, 3:随机规则化
	private final String[] modeDescriptions = {
		"固定地图(原始)",
		"固定地图(规则化后)",
		"随机地图(原始)",
		"随机地图(规则化后)"
	};

	// 纹理资源
	private Texture grassTexture;

	@Override
	public void create() {
		// 初始化图形组件
		batch = new SpriteBatch();
		camera = new OrthographicCamera();
		viewport = new FitViewport(960, 540, camera);
		camera.setToOrtho(false, 960, 540);

		// 加载纹理
		grassTexture = new Texture(Gdx.files.internal("sprites/Tileset.png"));

		// 初始化渲染器
		mapRenderer = new GdxMapRenderer(batch);
		mapRenderer.initializeWithTextureAtlas(grassTexture, 16);

		// 初始化规则处理器
		handler = new RegularizationHandler();
		RuleTile grassRule = RuleConfig.createGrassRuleTile();
		handler.registerRuleTile(grassRule);

		// 初始化地图数据
		initializeMapData();

		// 初始化字体
		font = generateFont(12 * 2, Color.WHITE);
	}

	private void initializeMapData() {
		// 固定地图数据
		fixedMapData =
				"0 g1 g1 g1 g1 0 0  g1 g1 0  0" + "\n" +
				"0 g1 g1 g1 g1 0 0  g1 g1 0  0" + "\n" +
				"0 g1 g1 0  g1 0 g1 g1 g1 g1 0" + "\n" +
				"0 g1 g1 g1 g1 0 g1 g1 g1 g1 0" + "\n" +
				"0 g1 g1 g1 g1 0 0  g1 g1 0  0";

		// 预计算规则化结果
		fixedProcessedMapData = handler.processMap(fixedMapData);

		// 生成初始随机地图
		currentRandomMapData = RuleConfig.createRandomMap(20, 15, 0.7);
		currentRandomProcessedMapData = handler.processMap(currentRandomMapData);
	}
	public static BitmapFont generateFont(int fntSize, Color color) {
		String fntPath = "fonts/ipix_12px_6000.ttf";
		// 加载字体
		FreeTypeFontGenerator generator = new FreeTypeFontGenerator(Gdx.files.internal(fntPath));
		FreeTypeFontGenerator.FreeTypeFontParameter parameter = new FreeTypeFontGenerator.FreeTypeFontParameter();
		parameter.size = fntSize; // 字体大小
		parameter.mono = false;
		parameter.incremental = true;
		parameter.color = color==null?Color.WHITE : color;
		return generator.generateFont(parameter);
	}

	@Override
	public void render() {
		ScreenUtils.clear(0.2f, 0.2f, 0.2f, 1f);

		batch.setProjectionMatrix(camera.combined);

		// 渲染地图
		renderCurrentMap();

		// 渲染UI文本
		renderUI();

		// 处理输入
		handleInput();
	}

	private void renderCurrentMap() {
		switch (renderMode) {
			case 0:
				mapRenderer.renderMap(fixedMapData);
				break;
			case 1:
				mapRenderer.renderMap(fixedProcessedMapData);
				break;
			case 2:
				mapRenderer.renderMap(currentRandomMapData);
				break;
			case 3:
				mapRenderer.renderMap(currentRandomProcessedMapData);
				break;
		}
	}

	private void renderUI() {
		batch.begin();

		// 模式提示
		font.draw(batch, "模式: " + modeDescriptions[renderMode], 10, viewport.getWorldHeight() - 30);

		// 操作提示
		font.draw(batch, "点击屏幕切换模式", 10, 30);

		batch.end();
	}

	private void handleInput() {
		if (Gdx.input.justTouched()) {
			switch (renderMode) {
				case 0:
					renderMode = 1; // 固定原始 → 固定规则化
					break;
				case 1:
					renderMode = 2; // 固定规则化 → 新随机地图
					currentRandomMapData = RuleConfig.createRandomMap(20, 15, 0.7);
					currentRandomProcessedMapData = handler.processMap(currentRandomMapData);
					break;
				case 2:
					renderMode = 3; // 随机原始 → 随机规则化
					break;
				case 3:
					renderMode = 2; // 随机规则化 → 新随机地图
					currentRandomMapData = RuleConfig.createRandomMap(20, 15, 0.7);
					currentRandomProcessedMapData = handler.processMap(currentRandomMapData);
					break;
			}
		}
	}

	@Override
	public void resize(int width, int height) {
		viewport.update(width, height);
	}

	@Override
	public void dispose() {
		batch.dispose();
		mapRenderer.dispose();
		grassTexture.dispose();
		font.dispose();
	}
}
